package com.qingyun.video.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingyun.apis.user.IUserClient;

import com.qingyun.model.common.dtos.R;
import com.qingyun.model.user.pojos.User;
import com.qingyun.model.video.dtos.ReplyDto;
import com.qingyun.model.video.pojos.Reply;
import com.qingyun.video.service.ReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/app/reply")
public class ReplyController {

    @Resource
    private ReplyService replyService;

    @Resource
    private IUserClient userClient;
    
    /**
     * 获取一级评论列表
     * @return 一级评论列表
     */
    @GetMapping("/parent/{blogId}")
    public R listParentComments(@PathVariable String blogId) {

        List<Reply> comments = replyService.listParentComments(Long.valueOf(blogId));
        return R.success(comments);
    }

    /**
     * 获取二级评论列表
     * @param parentId 父级评论ID
     * @return 二级评论列表
     */
    @GetMapping("/child")
    public R listChildComments(@RequestParam("parentId") String parentId,@RequestParam("id") String blogId) {
        List<Reply> comments = replyService.listChildComments(Long.valueOf(parentId),Long.valueOf(blogId));
        return R.success(comments);
    }



    /**
     * 保存
     */
    @PostMapping("/add")
    public R save(@RequestBody ReplyDto replyDto){
        Reply Reply = BeanUtil.copyProperties(replyDto, Reply.class);

        if (AppThreadLocalUtil.getUser()==null){
            return R.error("请登录后再发表评论~");
        }
        if (replyDto.getContent()==null||replyDto.getContent().trim().equals("")){
            return R.error("评论不能为空！");
        }
        Reply.setUserId(AppThreadLocalUtil.getUser().getId());
        boolean save = replyService.save(Reply);

            return R.success("评论成功，经验+3");

    }



    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R<String> delete(@RequestParam Long id){
        Reply reply = replyService.getById(id);
        LambdaQueryWrapper<Reply> queryWrapper = new LambdaQueryWrapper<>();
        if(reply.getReplyId() == null){
            queryWrapper.eq(Reply::getReplyId,id);
        } else {
            queryWrapper.eq(Reply::getParentId,id);
        }
        replyService.remove(queryWrapper);

        return R.success("删除成功");
    }

    /**
     * 删除评论
     */
    @RequestMapping("/delete/{id}")
    public R<String> deleteReplyById(@PathVariable Long id){
        Reply reply = replyService.getById(id);
        LambdaQueryWrapper<Reply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Reply::getId,id);
        replyService.remove(wrapper);
        LambdaQueryWrapper<Reply> queryWrapper = new LambdaQueryWrapper<>();

        if(reply.getReplyId() == null){
            queryWrapper.eq(Reply::getReplyId,id);
        } else {
            queryWrapper.eq(Reply::getParentId,id);
        }
        replyService.remove(queryWrapper);

        return R.success("删除成功");
    }




}
